package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.RegionLocator;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class _10_保险数据生成HFile {

    public static class HFileMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>.Context context) throws IOException, InterruptedException {
            String line = value.toString();
            //针对这一行数据进行切割处理
            String[] arr = line.split(",");
            //写出去
            /**
             * 7,211135,OsNvg,父母,XIXKmoEM,平安颐享-高端医疗险,Jnqgh,2012-11-20,2012-11-21,2012-11-22,2013-11-20,2013-11-20,5663,1084,56631084,现金,1
             * policy_no                                   -- 保单号
             * app_applicant_user_id                 -- 投保人id
             * applicant_name                          -- 投保人姓名
             * applicant_relation                       -- 与被保人关系
             * product_code                             -- 产品代码
             * product_name                            -- 产品名称
             * agent_code                                -- 主招揽业务员代码
             * application_date                         -- 投保日期
             * underwrite_date                         -- 承保日期
             * effective_date                            -- 生效日期
             * maturity_date                            -- 满期日期
             * payment_to_date                       -- 缴至日期
             * modal_standard_premium           -- 期缴标准保费
             * modal_add_premium                  -- 期缴其他加费
             * modal_total_premium                 -- 期缴保费合计
             * app_pay_type_code                   -- 支付类型
             * renewal_flag                             -- 续保标识(0:新保,1:续保)
             */
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f1".getBytes(),"app_applicant_user_id".getBytes(),arr[1].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f1".getBytes(),"applicant_name".getBytes(),arr[2].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"applicant_relation".getBytes(),arr[3].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"product_code".getBytes(),arr[4].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"product_name".getBytes(),arr[5].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"agent_code".getBytes(),arr[6].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"application_date".getBytes(),arr[7].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"underwrite_date".getBytes(),arr[8].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"effective_date".getBytes(),arr[9].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"maturity_date".getBytes(),arr[10].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"payment_to_date".getBytes(),arr[11].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"modal_standard_premium".getBytes(),arr[12].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"modal_add_premium".getBytes(),arr[13].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"modal_total_premium".getBytes(),arr[14].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"app_pay_type_code".getBytes(),arr[15].getBytes()));
            context.write(new ImmutableBytesWritable(arr[0].getBytes()),new KeyValue(arr[0].getBytes(),"f2".getBytes(),"renewal_flag".getBytes(),arr[16].getBytes()));
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        System.setProperty("HADOOP_USER_NAME","root");

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        Job job = Job.getInstance(conf);

        //设置mapper类
        job.setMapperClass(CreateHFileDemo.HFileMapper.class);

        //map的输出类型
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputValueClass(KeyValue.class);

        //设置reducetask的个数
        job.setNumReduceTasks(0);

        //输出成hfile，那你还需要告诉我具体给哪一个表输出
        //创建一个连接对象
        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("test:baoxian");
//        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
//        TableDescriptor build = tableDescriptorBuilder.build();
        Table table = conn.getTable(tableName);
        //就是hbase中具体的region的值
        RegionLocator regionLocator = conn.getRegionLocator(tableName);
        HFileOutputFormat2.configureIncrementalLoad(job,table,regionLocator);

        //设置输出的类型
        job.setOutputFormatClass(HFileOutputFormat2.class);

        //输入输出路径
        FileInputFormat.setInputPaths(job,new Path("D:\\output1.txt"));
        FileOutputFormat.setOutputPath(job,new Path("hdfs://linux01:8020/output1/"));

        //开启程序
        job.waitForCompletion(true);


    }
}
